<template>
  <div class="app-container">
    <!-- 查询条件区域-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
<!--      <el-form-item label="任务编码" prop="task_num">
        <el-input
          v-model="queryParams.task_num"
          placeholder="请输入任务编码"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>-->
      <el-form-item label="">
        <el-dropdown  @command="bglxs">
          <el-button type="primary" size="min" style="width: 100px;
            height: 32px;
            border: 1px solid #1890ff;
            border-radius: 6px;
            font-size: 12px;
            color: #C1C1C1;">
            {{ bglx }}<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown" style="background-color: #122a64;border-color: #1890ff;">
            <el-dropdown-item style="color: #5DCBFF;" command="普通任务" @click.native="zdbg()">普通任务</el-dropdown-item>
            <el-dropdown-item style="color: #5DCBFF;" command="特巡任务" @click.native="txbg()">特巡任务</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>
      <el-form-item label="任务名称" prop="task_name">
        <el-input   style="width: 145px;"
          v-model="queryParams.task_name"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>
<!--      <el-form-item label="机器人名称" prop="robot_name">
        <el-input  style="width: 150px;"
          v-model="queryParams.robot_name"
          placeholder="请输入机器人名称"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />-->
      <el-form-item label="创建时间">
        <el-date-picker
            v-model="queryParams.create_times"
            style="width: 215px" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
     <!--增删改查按钮区域-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['nssc:patroltask:add']"
        >新增</el-button>
      </el-col>
<!--     <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="success"-->
<!--            plain-->
<!--            icon="el-icon-folder-checked"-->
<!--            size="mini"-->
<!--            :disabled="multiple"-->
<!--            @click="handleAuto"-->
<!--            v-hasPermi="['nssc:patroltask:add']"-->
<!--        >设置自动</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--      <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-folder-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleUAuto"-->
<!--          v-hasPermi="['nssc:patroltask:add']"-->
<!--      >取消自动</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['robot:patroltask:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-s-operation"
            size="mini"
            :disabled="multiple"
            @click="handleRestore"
            v-hasPermi="['robot:patroltask:edit']"
        >恢复任务</el-button>
      </el-col>-->
     <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
     <!-- 数据列表区域-->
    <el-table v-loading="loading" :data="patroltaskList" @selection-change="handleSelectionChange" :cell-style="{borderColor:'rgb(1 5 43)'}" :header-cell-style="{borderColor: 'rgb(1 5 43)'}" >
      <el-table-column type="selection" width="55" align="center" />
    <!--  <el-table-column label="序号" type="index" align="center" prop="id" width="100" />-->
      <el-table-column label="任务名称" align="center" prop="task_name" />
<!--      <el-table-column label="机器人名称" align="center" prop="robot_name"/>-->
      <!--<el-table-column label="执行时刻" align="center" prop="execution_time" width="90" />-->
      <!--<el-table-column label="执行时间(H)" align="center" prop="execution_time" width="110" />-->
<!--      <el-table-column label="执行时刻(H)" align="center" prop="execution_time">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.execution_time!=''&&scope.row.execution_time!=null">{{scope.row.execution_time}}H</el-tag>
          <span style="color: #1952e3" v-else>&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;</span>
        </template>
      </el-table-column>-->

      <el-table-column label="速度(m/min)" align="center" prop="speed">
        <template slot-scope="scope">
          <span>{{scope.row.speed}}m/min</span>
        </template>
      </el-table-column>
      <el-table-column label="巡检类型" align="center" prop="patrol_type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.patrol_type==1">反冲洗巡检</el-tag>
          <el-tag v-else-if="scope.row.patrol_type==2">气洗巡检</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="状态" align="center" prop="task_type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.task_state==0">未下发</el-tag>
          <el-tag v-else-if="scope.row.task_state==1">下发执行</el-tag>
          <el-tag v-else>已执行</el-tag>
        </template>
      </el-table-column>-->
      <el-table-column label="创建时间" align="center" prop="creater_time" width="170">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creater_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="280">
        <template slot-scope="scope">
<!--          <el-button
              size="mini"
              type="text"
              icon="el-icon-circle-check"
              @click="handleRestore(scope.row)"
              v-hasPermi="['nssc:patrolpoint:edit']"
          >下发任务</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-circle-close"
              @click="handleCancel(scope.row)"
              v-hasPermi="['nssc:patrolpoint:edit']"
          >撤销任务</el-button>
          <el-button  v-if="scope.row.task_type==1"
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleTaskPoint(scope.row)"
            v-hasPermi="['nssc:patrolpoint:query']"
          >任务详情</el-button>-->
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['nssc:patrolpoint:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--列表分页区域-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" style="background:rgb(1 5 43);"
    />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="630px" @close="handleCancle" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
<!--          <el-col :span="12">
            <el-form-item label="机器人" prop="robot_config_id">
              <el-select v-model="form.robot_config_id" clearable  placeholder="请选机器人">
                <el-option
                  v-for="item in robotList"
                  :key="item.id"
                  :label="item.robot_name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>-->
          <el-col :span="12">
          <el-form-item label="巡检类型" prop="patrol_type">
            <el-select v-model="form.patrol_type" placeholder="请选巡检类型"  @change="getxjlx">
              <el-option
                  v-for="item in patrols_type"
                  :key="item.patrol_type_num"
                  :label="item.patrol_type_name"
                  :value="item.patrol_type_num"
              ></el-option>
            </el-select>
          </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务名称" prop="task_name">
              <el-input v-model="form.task_name" placeholder="请输入任务名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="巡检速度" prop="speed">
              <el-select v-model="form.speed" placeholder="请选择机器人速度">
                <el-option
                    v-for="sp in speeds"
                    :key="sp"
                    :label="sp"
                    :value="sp"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="执行时刻" prop="execution_time" style="display: block">-->
<!--              <el-select v-model="form.execution_time" placeholder="请选择执行时刻(小时)" @change="getOptionszxsj" >-->
<!--                <el-option-->
<!--                    v-for="sj in zxsk"-->
<!--                    :key="sj"-->
<!--                    :label="sj"-->
<!--                    :value="sj"-->
<!--                ></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item prop="residence_time">
                <span slot="label">
                <span style="color: red">* </span><span>停留时间</span>
              </span>
              <el-input id="residencetime" v-model="form.residence_time" placeholder="请输入停留时间1x10(秒)" maxlength="1" show-word-limit  clearable/>
            </el-form-item>
          </el-col>-->
          <el-col :span="24">
            <el-form-item label="巡检说明">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容(0-255)"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" id="patrolid">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addPatrolTask,
  AutoPatrolTask, cancelTask,
  delPatrolTask,
  getPatrolTask,
  getPatrolTaskPoint,
  getPatrolTaskPoints,
  listPatrolTask,
  listPatrolTasks, listPatrolTXTask,
  restoreTask,
  UAutoPatrolTask
} from "@/api/nssc/patroltask/patroltask";
import {listRobotConfig} from "@/api/nssc/robotconfig/robotconfig";
import $ from "jquery";
import "@/assets/styles/robt-bg.css";
export default {
  data() {
    return {
      execution_time:null, //日期选择
      pickerOption: {
        disabledDate: time => {
          const dateVal = new Date(this.execution_time).getTime();
          if (dateVal) {
            return time.getTime() > dateVal + 1;
          }
        }
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      task_names:[],
      bglx: "特巡任务",
      // 选中巡检点数组
      point_ids: [],
      pond_nums:[],
      label_nums:[],
      position_dists:[],
      xypositions:[],
      position_nums:[],
      points_types:[],
      points_type:[],
      timer:null,//定时器
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //任务列表
      typeList: [],
      //机器人列表
      robotList:[],
      //巡检任务表格数据
      patroltaskList: [],
      zxsk:['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23'],
      speeds:['1000','2000','3000','4000'],
      //巡检类型
      patrols_type:[
          {patrol_type_num:1,patrol_type_name:'反冲洗'},{patrol_type_num:2,patrol_type_name:'气洗'}
      ],
      pool_nums:[
          {pond_num:1,pond_name:'R滤格'},{pond_num:2,pond_name:'S滤格'},{pond_num:3,pond_name:'T滤格'},{pond_num:4,pond_name:'U滤格'},
          {pond_num:5,pond_name:'V滤格'},{pond_num:6,pond_name:'W滤格'},{pond_num:7,pond_name:'X滤格'},{pond_num:8,pond_name:'Y滤格'}
      ],
      point_types:[
          {point_type:"1",point_type_name:'漂浮物'},{point_type:"2",point_type_name:'青苔'},
          {point_type:"3",point_type_name:'安全帽'},{point_type:"4",point_type_name:'阀门状态'},
          {point_type:"5",point_type_name:'曝气'},{point_type:"6",point_type_name:'砂面'}
      ],
      checkList: [],
      //巡检任务巡检点表格数据
      patroltaskpointList:[],
      // 弹出层标题
      title: "",
      titlepoint: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      open1: false,
      videotape_time: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        task_name: undefined,
        task_num: undefined,
        robot_name: undefined,
        task_type: undefined,
        execution_time:undefined,
        residence_time:undefined,
        create_times: [],
      },
      // 表单参数
      form: {},
      // 表单参数
      form1: {},
      // 表单校验
      rules: {
        task_name: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ] ,
       /* robot_config_id: [
          { required: true, message: "机器人名称不能为空"}
        ],*/
        task_type: [
          { required: true, message: "任务类型不能为空", trigger: "blur" }
        ],
       /* residence_time: [
          { required: true, message: "停留时间不能为空", trigger: "blur" }
        ],*/
    /*    inspect_type: [
          { required: true, message: "巡检视频类型不能为空"}
        ],*/
        speed: [
          { required: true, message: "机器人速度不能为空"}
        ],
        pool_num: [
          { required: true, message: "池子编号不能为空"}
        ],
        patrol_type:[
          { required: true, message: "巡检类型不能为空"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.timer = setInterval(()=>{
      this.getList(); //刷新站点列表
    },7000);//7秒
  },
  beforeDestroy() {
    //清除定时器
    if(this.timer) {
      clearInterval(this.timer); //关闭
    }
  },
  methods: {
    zdbg() {
      this.$router.push('/nssc/task/pttask');
    },
    txbg() {
      this.$router.push('/nssc/task/txtask');
    },
    bglxs(bglx) {
      this.bglx = bglx;
    },
    //根据机器人id、执行时间查询是否有关联任务
    getxjlx(patrol_type) {
      this.queryParams.patrol_type = patrol_type;
      listPatrolTXTask(this.queryParams).then(response => {
        if(typeof(response)!="undefined" && response != null && response.rows.length>0) {
          $("#patrolid").css("display","none");
          this.$modal.confirm1('当前类型任务已创建！');
        }else {
          $("#patrolid").css("display","");
        }
      });
      this.queryParams.patrol_type = undefined;
    },
    //自定义对话框关闭方法
    handleCancle () {
      $("#patrolid").css("display","");
      this.form.execution_time = undefined;
      this.queryParams.execution_time = undefined;
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    //根据机器人id、执行时间查询是否有关联任务
   /* getOptionszxsj(execution_time) {
      this.queryParams.execution_time = execution_time;//设置站点id条件，查询工艺体
      listPatrolTasks(this.queryParams).then(response => {
        if(typeof(response)!="undefined" && response != null && response.rows.length>0) {
          $("#patrolid").css("display","none");
          this.$modal.confirm1('当前执行时间任务已创建！');
        }else {
          $("#patrolid").css("display","");
        }
      });
    },*/
    /** 查询机器人列表 */
    getrobotList() {
      listRobotConfig(this.queryParams).then(response => {
        this.robotList = response.rows;
      });
    },
    /** 查询巡检任务列表 */
    getList() {
      this.loading = true;
      listPatrolTXTask(this.queryParams).then(response => {
        this.patroltaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        point_name: undefined,
        point_num:undefined,
        robot_name:undefined,
        patrol_type:undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.create_times= [];
      this.queryParams.patrol_type=undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.task_names = selection.map(item => item.task_name)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    // 多选框选中点位数据
    handleSelectionChangePatrolPoint(selection) {
      this.point_ids = selection.map(item => item.id);
      this.pond_nums = selection.map(item => item.pond_num);
      this.label_nums = selection.map(item => item.label_num);
      this.position_dists = selection.map(item => item.position_dist);
      this.xypositions = selection.map(item => item.xyposition);
      this.position_nums = selection.map(item => item.position_num);
      this.points_type = selection.map(item => item.point_type);
      this.points_types = [];//清空
      for (let i = 0; i <this.points_type.length; i++) {
          this.points_types[i] = this.points_type[i].toString();
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.patrolpointList=[];
      this.reset();
      this.open = true;
      this.title = "新增任务";
      this.getrobotList();//机器人列表
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPatrolTask(id).then(response => {
        //反显的时候用，手动调用 start
        this.getOptions(response.data.site_id);
        //end
        this.form = response.data;
        this.open = true;
        this.title = "修改任务";
      });
      //该机器人所有的巡检点
      getPatrolTaskPoints(id).then(response => {
        this.patrolpointList = response.list;
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.point_ids = this.point_ids;
          this.form.pond_nums = this.pond_nums;
          this.form.label_nums = this.label_nums;
          this.form.position_dists = this.position_dists;
          this.form.xypositions = this.xypositions;
          this.form.position_nums = this.position_nums;
          this.form.points_types = this.points_types;
          this.form.task_type = 2;
        /* //停留时间
          let residencetime = $("#residencetime").val();
          if(residencetime == undefined || residencetime == null || residencetime == "") {
            this.form.residencetime = undefined;
            this.$modal.confirm1('停留时间不能为空');
            return false;
          }*///新增
          addPatrolTask(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /**删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const task_names =row.task_name || this.task_names
      this.$modal.confirm('是否确认删除任务名称为"' + task_names + '"的数据项？').then(function() {
        return delPatrolTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /**自动任务按钮操作 */
    handleAuto(row) {
      const ids = row.id || this.ids;
      const task_names =row.task_name || this.task_names
      this.$modal.confirm('设置自动任务 "' + task_names + '" ？').then(function() {
        return AutoPatrolTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("设置成功");
      }).catch(() => {});
    },
    /**取消自动任务按钮操作 */
    handleUAuto(row) {
      const ids = row.id || this.ids;
      const task_names =row.task_name || this.task_names
      this.$modal.confirm('取消自动任务 "' + task_names + '" ？').then(function() {
        return UAutoPatrolTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消成功");
      }).catch(() => {});
    },
    /**下发任务按钮操作 */
    handleRestore(row) {
      const ids = row.id || this.ids;
      const task_names =row.task_name || this.task_names
      this.$modal.confirm('是否确认下发 "' + task_names + '" 的任务项？').then(function() {
        return restoreTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("下发任务成功");
      }).catch(() => {});
    },
    /**撤销任务按钮操作 */
    handleCancel(row) {
      const ids = row.id || this.ids;
      const task_names =row.task_name || this.task_names
      this.$modal.confirm('是否撤销 "' + task_names + '" 的任务项？').then(function() {
        return cancelTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("撤销任务成功");
      }).catch(() => {});
    },
    /**点位详情按钮操作 */
    handleTaskPoint(row) {
      const id = row.id || this.ids
      getPatrolTaskPoint(id).then(response => {
        if(typeof response.rows !='undefined' && response.rows!=null && response.rows!=''){
          this.patroltaskpointList = response.rows;
        }else {
          this.patroltaskpointList =[];
        }
        this.open1 = true;
        this.titlepoint = "巡检点详细";
      });
    },//取消按钮
    cancel1() {
      this.open1 = false;
      this.reset();
    },
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
/**列表添加垂直隐藏滚动条**/
.tabled{
height: 550px;
overflow: hidden;
overflow-y: scroll;
}
.tabled::-webkit-scrollbar {
width: 0 !important;
height: 0 !important;
}
.el-dropdown-menu__item:not(.is-disabled):hover, .el-dropdown-menu__item:focus {
  background-color: #0b52bd;
}
</style>
